<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board
|   =============================================
|   by Matthew Mecham
|   (c) 2001 - 2006 Invision Power Services, Inc.
|   
|   =============================================
|   
|   
+---------------------------------------------------------------------------
|   > $Date: 2010-06-22 12:27:54 -0400 (Mon, 22 Jun 2010) $
|   > $Revision: 01 $
|   > $Author: BabyWolf $
+---------------------------------------------------------------------------
|
|   > TASK SCRIPT: Reward top posters
|   > Script written by BabyWolf
|   > Date started: 03rd August 2008 (12:27)
|
+--------------------------------------------------------------------------
*/

//-----------------------------------------
// THIS TASKS OPERATIONS:
// reward top posters everyday
//+----------------------------------------

if ( ! defined( 'IN_IPB' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
	exit();
}

class task_item
{
	var $class     = "";
	var $root_path = "";
	var $task      = "";
	
	/*-------------------------------------------------------------------------*/
	// Our 'auto_run' function
	// ADD CODE HERE
	/*-------------------------------------------------------------------------*/
	
	function run_task()
	{
		$time_high = time();
 		$ids       = array();
 		$time_low  = $time_high - (60*60*24);
 		
 		//-----------------------------------------
		// Query the DB
		//-----------------------------------------
	
		foreach( $this->ipsclass->forums->forum_by_id as $id => $data )
		{
			if ( ! $data['inc_postcount'] )
			{
				continue;
			}
		
			$ids[] = $id;
		}
		
		if( count( $ids ) )
		{
			$this->ipsclass->DB->cache_add_query( 'stats_get_todays_posters', array( 'ids' => $ids, 'time_low' => $time_low ) );
			$this->ipsclass->DB->cache_exec_query();
    		
			$count = 0;
			while ($r = $this->ipsclass->DB->fetch_row())
			{
				$count++;
				if($count > $this->ipsclass->vars['dkm_topnumber']) break;
				$member_ids[] = $r['id'];
			}
			
			if($count > 0) {
				// Reward them
				// Build set string
				$setStr = 'field_'.$this->ipsclass->vars['dkm_cashid']."=".'field_'.$this->ipsclass->vars['dkm_cashid']."+".$this->ipsclass->vars['dkm_cashreward'];
				// Construct whole query
				$this->ipsclass->DB->simple_construct(array('update' 	=> 'pfields_content',
															'set'		=> $setStr,
															'where'		=> 'member_id IN('.implode(",",$member_ids).')',
															)
														);
				$this->ipsclass->DB->simple_exec();
			}
		}
		
		$this->class->unlock_task( $this->task );
		$this->class->append_task_log( $this->task, 'Member rewarded.' );
				
		//-----------------------------------------
		// Unlock Task: DO NOT MODIFY!
		//-----------------------------------------
		
		$this->class->unlock_task( $this->task );
	}
	
	/*-------------------------------------------------------------------------*/
	// register_class
	// LEAVE ALONE
	/*-------------------------------------------------------------------------*/
	
	function register_class(&$class)
	{
		$this->class     = &$class;
		$this->ipsclass  =& $class->ipsclass;
		$this->root_path = $this->class->root_path;
	}
	
	/*-------------------------------------------------------------------------*/
	// pass_task
	// LEAVE ALONE
	/*-------------------------------------------------------------------------*/
	
	function pass_task( $this_task )
	{
		$this->task = $this_task;
	}
	
	
}
?>